#include <vector>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>

unsigned long GetLocalTimeMSec()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000 + tv.tv_usec;
}

void GenRandomList(std::vector<int>& iVector)
{
    int seed = 1;
    for (int i=0; i<100; i++)
    {
        int ele = (seed * GetLocalTimeMSec()) % 999;
        iVector.push_back(ele);
        if (ele == 0)
        {
            seed = GetLocalTimeMSec();
        }
        else
        {
            seed = ele;
        }
        usleep(9*99);
    }
}

void display(const std::vector<int>& iVector)
{
    std::vector<int>::const_iterator iter = iVector.begin();
    int count = 0;
    while (iter != iVector.end())
    {
        printf("%4d ", *iter);
        iter++;
        count++;
        if (count >=20)
        {
            count=0;
            printf("\n");
        }
        
    }
    printf("\n");
}

/*
插入排序，属于就地排序
*/
void mysort(std::vector<int>& iVector)
{
    for (size_t i = 1; i < iVector.size(); i++)
    {
        int ele = iVector[i];
        int index;
        for (index = i-1; index >= 0 && ele<iVector[index]; index--)
        {
            iVector[index+1] = iVector[index];
        }

        if (i != index)
        {
            iVector[index+1] = ele;
        }
        
        printf("第%d次：",i), display(iVector);
    }
}


template <typename T>
void removeDup(std::vector<T>&v)
{
    T currValue;
    typename std::vector<T>::const_iterator currIter, tmpIter;
    currIter = v.begin();
    while (currIter != v.end())
    {
        currValue = *currIter;
        tmpIter = currIter;
        tmpIter++;
        while (tmpIter != v.end())
        {
            if (*tmpIter == currValue)
            {
                tmpIter = v.erase(tmpIter);
            }
            else
            {
                tmpIter++;
            }
        }
        currIter++;
    }
}

// template <typename T>
// std::vector<T> reverseVector(const std::vector<T>& v)
// {
//     std::vector<T> rev;
//     int tail = v.size();
//     for (int i = tail-1; i >=0 ; i--)
//     {
//         rev.push_back(v[i]);
//     }
//     return rev;
// }
int main()
{
    std::vector<int> v={1,7,3,9,5,6,7,8,9};
    display(v);
    removeDup(v);
    display(v);
    
    return 0;
}